﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Framework.Data;
using Dal.Entities;

namespace Web.Controllers
{
    [ValidateInput(false)]
    public class IndexController : Controller
    {
        //
        // GET: /Index/

        public ActionResult Index()
        {
            var userid = Common.GetUserID();
            var doctype = Common.DocType();
            bool isAdmin = Common.Admin();
            var rep = RepositoryFactory.GetRepository();
            var listTmp = from doc in rep.GetQuery<Doc>()
                          join doc0 in rep.GetQuery<Doc0>()
                          on doc.DocID equals doc0.DocID
                          join user in rep.GetQuery<User>()
                          on doc.UserID equals user.ID
                          where doc.StatusValid == 1
                          orderby doc.DocID descending
                          select new HomeDoc
                          {
                              Doc = doc,
                              PatientName = doc0.PatientName,
                              UserName = user.UserName,
                              UserID = user.ID
                          };
            if (!isAdmin)
                listTmp = listTmp.Where(i => i.UserID == userid);

            var list = listTmp.ToList();

            ViewBag.NotComplete = list.Where(i => i.Doc.StatusComplete == 0).ToList();
            ViewBag.NotAudit = list.Where(i => i.Doc.StatusAudit == 0).ToList();
            ViewBag.Question = list.Where(i => i.Doc.StatusQuestion == 1).ToList();
            return View();
        }


        public ActionResult Suifang(int? weeks)
        {
            var userid = Common.GetUserID();
            bool isAdmin = Common.Admin();
            var rep = RepositoryFactory.GetRepository();
            switch (weeks)
            {
                case 2:
                    {
                        var t2 = DateTime.Now.AddDays(-14);
                        var tlt2 = t2.AddDays(-14);
                        var listTmp2 = from doc in rep.GetQuery<Doc>()
                                       join user in rep.GetQuery<User>()
                                       on doc.UserID equals user.ID
                                       join doc0 in rep.GetQuery<Doc0>()
                                       on doc.DocID equals doc0.DocID
                                       join doc2 in rep.GetQuery<Doc2>()
                                       on doc.DocID equals doc2.DocID
                                       where doc.StatusValid == 1 && doc2.StatusComplete == 0 && doc0.InputTime < t2 && doc0.InputTime > tlt2
                                       select new HomeDoc
                                       {
                                           Doc = doc,
                                           PatientName = doc0.PatientName,
                                           InputTime = doc0.InputTime,
                                           UserName = user.UserName,
                                           UserID = user.ID,
                                           VisitDesc = "2周随访",
                                           Weeks = weeks
                                       };
                        if (!isAdmin)
                            listTmp2 = listTmp2.Where(i => i.UserID == userid);
                        return View(listTmp2.ToList());
                    }
                case 8:
                    {
                        var t8 = DateTime.Now.AddDays(-56);
                        var tlt8 = t8.AddDays(-14);
                        var listTmp8 = from doc in rep.GetQuery<Doc>()
                                       join user in rep.GetQuery<User>()
                                       on doc.UserID equals user.ID
                                       join doc0 in rep.GetQuery<Doc0>()
                                       on doc.DocID equals doc0.DocID
                                       join doc3 in rep.GetQuery<Doc3>()
                                       on doc.DocID equals doc3.DocID
                                       where doc.StatusValid == 1 && doc3.StatusComplete == 0 && doc0.InputTime < t8 && doc0.InputTime > tlt8
                                       select new HomeDoc
                                       {
                                           Doc = doc,
                                           PatientName = doc0.PatientName,
                                           InputTime = doc0.InputTime,
                                           UserName = user.UserName,
                                           UserID = user.ID,
                                           VisitDesc = "8周随访",
                                           Weeks = weeks
                                       };
                        if (!isAdmin)
                            listTmp8 = listTmp8.Where(i => i.UserID == userid);
                        return View(listTmp8.ToList());
                    }
                case 24:
                    {
                        var t24 = DateTime.Now.AddDays(-56);
                        var tlt24 = t24.AddDays(-14);
                        var listTmp24 = from doc in rep.GetQuery<Doc>()
                                        join user in rep.GetQuery<User>()
                                        on doc.UserID equals user.ID
                                        join doc0 in rep.GetQuery<Doc0>()
                                        on doc.DocID equals doc0.DocID
                                        join doc4 in rep.GetQuery<Doc4>()
                                        on doc.DocID equals doc4.DocID
                                        where doc.StatusValid == 1 && doc4.StatusComplete == 0 && doc0.InputTime < t24 && doc0.InputTime > tlt24
                                        select new HomeDoc
                                        {
                                            Doc = doc,
                                            PatientName = doc0.PatientName,
                                            InputTime = doc0.InputTime,
                                            UserName = user.UserName,
                                            UserID = user.ID,
                                            VisitDesc = "6月随访",
                                            Weeks = weeks
                                        };
                        if (!isAdmin)
                            listTmp24 = listTmp24.Where(i => i.UserID == userid);
                        return View(listTmp24.ToList());
                    }
                default:
                    {
                        var t2 = DateTime.Now.AddDays(-14);
                        var tlt2 = t2.AddDays(-14);
                        var t8 = DateTime.Now.AddDays(-56);
                        var tlt8 = t8.AddDays(-14);
                        var t24 = DateTime.Now.AddDays(-56);
                        var tlt24 = t24.AddDays(-14);
                        var listTmpAll = from doc in rep.GetQuery<Doc>()
                                         join user in rep.GetQuery<User>()
                                         on doc.UserID equals user.ID
                                         join doc0 in rep.GetQuery<Doc0>()
                                         on doc.DocID equals doc0.DocID
                                         join doc2 in rep.GetQuery<Doc2>()
                                         on doc.DocID equals doc2.DocID
                                         join doc3 in rep.GetQuery<Doc3>()
                                         on doc.DocID equals doc3.DocID
                                         join doc4 in rep.GetQuery<Doc4>()
                                         on doc.DocID equals doc4.DocID
                                         where doc.StatusValid == 1 &&
                                         (
                                            (doc2.StatusComplete == 0 && doc0.InputTime < t2 && doc0.InputTime > tlt2) ||
                                            (doc3.StatusComplete == 0 && doc0.InputTime < t8 && doc0.InputTime > tlt8) ||
                                            (doc4.StatusComplete == 0 && doc0.InputTime < t24 && doc0.InputTime > tlt24)
                                         )
                                         select new HomeDoc
                                         {
                                             Doc = doc,
                                             PatientName = doc0.PatientName,
                                             UserName = user.UserName,
                                             UserID = user.ID,
                                             InputTime = doc0.InputTime,
                                             Doc2StatusComplete = doc2.StatusComplete,
                                             Doc3StatusComplete = doc3.StatusComplete,
                                             Doc4StatusComplete = doc4.StatusComplete
                                         };
                        if (!isAdmin)
                            listTmpAll = listTmpAll.Where(i => i.UserID == userid);
                        return View(listTmpAll.ToList());
                    }
            }
        }

        public class HomeDoc
        {
            public Doc Doc { get; set; }
            public string PatientName { get; set; }
            public string UserName { get; set; }
            public int UserID { get; set; }
            public int? Weeks { get; set; }
            private string visitDesc = string.Empty;
            public string VisitDesc
            {
                get
                {
                    if (string.IsNullOrWhiteSpace(visitDesc))
                    {
                        if (Doc2StatusComplete == 0 && InputTime < DateTime.Now.AddDays(-14) && InputTime > DateTime.Now.AddDays(-14).AddDays(-14))
                        {
                            visitDesc += "2周随访";
                        }
                        if (Doc3StatusComplete == 0 && InputTime < DateTime.Now.AddDays(-56) && InputTime > DateTime.Now.AddDays(-56).AddDays(-14))
                        {
                            visitDesc += "8周随访";
                        }
                        if (Doc4StatusComplete == 0 && InputTime < DateTime.Now.AddDays(-56) && InputTime > DateTime.Now.AddDays(-56).AddDays(-14))
                        {
                            visitDesc += "6月随访";
                        }
                    }
                    return visitDesc;
                }
                set
                {
                    visitDesc = value;
                }
            }
            public int? Doc2StatusComplete { get; set; }
            public int? Doc3StatusComplete { get; set; }
            public int? Doc4StatusComplete { get; set; }
            public DateTime? InputTime { get; set; }
            public DateTime DeadLineTime
            {
                get
                {
                    if (InputTime < DateTime.Now.AddDays(-14) && InputTime > DateTime.Now.AddDays(-14).AddDays(-14))
                    {
                        return InputTime.Value.AddDays(14).AddDays(14);
                    }
                    if (InputTime < DateTime.Now.AddDays(-56) && InputTime > DateTime.Now.AddDays(-56).AddDays(-14))
                    {
                        return InputTime.Value.AddDays(56).AddDays(14);
                    }
                    if (InputTime < DateTime.Now.AddDays(-56) && InputTime > DateTime.Now.AddDays(-56).AddDays(-14))
                    {
                        return InputTime.Value.AddMonths(6).AddDays(14);
                    }
                    return DateTime.Now;
                }
            }
        }
    }
}
